<?php

/*
 * This file is part of ibrand/EC-Open-Server.
 *
 * (c) iBrand <https://www.ibrand.cc>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace iBrand\EC\Open\Server\Http\Controllers;

use iBrand\Component\Address\RepositoryContract as AddressRepository;
use Validator;

class AddressController extends Controller
{
    protected $addressRepository;

    public function __construct(AddressRepository $addressRepository)
    {
        $this->addressRepository = $addressRepository;
    }

    public function index()
    {
        $list = [];
        $AddressList = $this->addressRepository->getByUser(request()->user()->id);
        if(($AddressList)){
            $list = [];
            foreach ($AddressList as $value){
                $arr['id'] = $value['id'];
                $arr['phone'] = $value['mobile'];
                $arr['name'] = $value['accept_name'];
                $arr['province'] = $value['address_name'];
                $arr['detail'] = $value['address'];
                $arr['is_default'] = $value['is_default'];
                $list[] = $arr;
            }
        }
        return $this->success($list);
    }



    //添加地址
    public function addreceiv()
    {

        $name = request('name');
        $phone = request('phone');
        $province = request('province');
        $detail = request('detail');
        $is_default = request('is_default');
        $user_id = request()->user()->id;

        $res = [
            'user_id' => $user_id,
            'accept_name' => $name,
            'mobile' => $phone,
            'address_name' => $province,
            'address' => $detail,
            'is_default' =>isset($is_default)?$is_default:'0'
        ];
        $res = $this->addressRepository->create($res);
        if(!$res){return $this->failed('创建地址失败');}
        return $this->success('创建成功！');
    }

    //修改地址
    public function editreceiv()
    {
        $name = request('name');
        $phone = request('phone');
        $province = request('province');
        $detail = request('detail');
        $is_default = request('is_default');
        $address_id = request('address_id');
        $user_id = request()->user()->id;

        if(!$address_id){
            return $this->failed('修改地址失败!');
        }
        $res = [
            'accept_name' => $name,
            'mobile' => $phone,
            'address_name' => $province,
            'address' => $detail,
            'is_default' =>isset($is_default)?$is_default:'0'
        ];
        $res = $this->addressRepository->update($res,$address_id);
        if(!$res){return $this->failed('修改地址失败');}
        return $this->success('修改成功！');
    }



    public function store()
    {
        $input = request()->all();

        $validator = Validator::make($input, [
            'accept_name' => 'required',
            'mobile' => 'required',
            'address_name' => 'required',
            'address' => 'required',
            'is_default' => 'required',
        ]);

        if ($validator->fails()) {
            return $this->failed($validator->errors());
        }

        $input['user_id'] = request()->user()->id;

        if (!$address = $this->addressRepository->create($input)) {
            return $this->failed('创建地址失败');
        }
        return $this->success('创建成功！');
    }

    public function update()
    {
        $user_id = request()->user()->id;

        $id = request()->only(['address_id']);
        $update_address = request()->only(['accept_name', 'mobile', 'address_name', 'address', 'is_default']);

        if(!$id['address_id']){
            return $this->failed('修改地址失败!');
        }

        if (!$address = $this->addressRepository->updateByUser($update_address, $id['address_id'], $user_id)) {
            return $this->failed('修改地址失败');
        }

        return $this->success('修改成功！');
    }

    public function show($id)
    {
        $user_id = request()->user()->id;

        if (!$address = $this->addressRepository->findWhere(['id' => $id, 'user_id' => $user_id])->first()) {
            return $this->failed('获取收货地址失败');
        }

        return $this->success($address);
    }

    public function default()
    {
        $user_id = request()->user()->id;

        $defaultAddress = $this->addressRepository->getDefaultByUser($user_id);
        if($defaultAddress){
            $arr['id'] = $defaultAddress['id'];
            $arr['phone'] = $defaultAddress['mobile'];
            $arr['name'] = $defaultAddress['accept_name'];
            $arr['province'] = $defaultAddress['address_name'];
            $arr['detail'] = $defaultAddress['address'];
            $arr['is_default'] = $defaultAddress['is_default'];
            return $this->success($arr);
        }else{
            return $this->failed('暂无地址');
        }


    }

    public function delete($id)
    {
        $user_id = request()->user()->id;

        if (!$this->addressRepository->deleteWhere(['id' => $id, 'user_id' => $user_id])) {
            return $this->failed('删除收货地址失败');
        }

        return $this->success();
    }
}
